package com.ohc.leetcode.数组.repect;

/**
 * 2022年8月12日22:05:29
 */
public class 长度最小的子数组201 {

    public static void main(String[] args) {
        int[] ints = new int[]{2, 3, 1, 2, 4, 3};
        int i = minSubArrayLen(7, ints);
        System.out.println("i = " + i);

    }

    public static int minSubArrayLen(int target, int[] nums) {
        int left = 0;
        int right = 0;
        int result = 0;
        int bestResult = 0;
        while (right < nums.length) {
            result = result + nums[right];
            while (result >= target) {
                if (right - left + 1 < bestResult || bestResult == 0) {
                    bestResult = right - left + 1;
                }
                result = result - nums[left];
                left++;
            }
            right++;
        }

        return bestResult;
    }
}
